//
//  MineLoginDeleteViewController.m
//  ShowYork
//
//  Created by hxh on 2020/10/26.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineLoginDeleteViewController.h"
#import "DiscoverListViewController.h"
@interface MineLoginDeleteViewController ()
@property (nonatomic,strong)UILabel *titleTotalLb;
@property (nonatomic,strong)UILabel *titleMainLb;
@property (nonatomic,strong)UILabel *titleDescLb;
@property (nonatomic,strong)UILabel *titleSubTitleLb;
@property (nonatomic,strong)UILabel *titleSubDescLb;
@property (nonatomic,strong)UIImageView *checkImgView;
@property (nonatomic,strong)UILabel *remindTitleLb;
@property (nonatomic,strong)UIButton *checkBtn;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,strong)CAGradientLayer *loginBtnGradientLayer;
@end

@implementation MineLoginDeleteViewController


- (CAGradientLayer *)loginBtnGradientLayer{
    if (!_loginBtnGradientLayer) {
        _loginBtnGradientLayer =  [CAGradientLayer layer];
        _loginBtnGradientLayer.frame = self.confirmBtn.bounds;
        _loginBtnGradientLayer.endPoint = CGPointMake(0, 0);
        _loginBtnGradientLayer.startPoint  = CGPointMake(1.0, 0);
        _loginBtnGradientLayer.colors = @[(id)RGBA(119, 153, 255, 1).CGColor,(id)RGBA(86, 128, 250, 1).CGColor];
    }
    return _loginBtnGradientLayer;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
  
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
    // Do any additional setup after loading the view.
}

- (void)setUI{
    self.navigationItem.title = @"注销帐号";
    self.titleTotalLb = [[UILabel alloc]init];
    self.titleTotalLb.text = @"注销帐号存在以下风险";
    self.titleTotalLb.textColor = RGBA(35, 24, 21, 1);
    self.titleTotalLb.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:self.titleTotalLb];
    [self.titleTotalLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.top.equalTo(self.view).offset(28 + kNavBarHeight);
    }];
    
    self.titleMainLb = [[UILabel alloc]init];
    self.titleMainLb.text = @"永久注销，无法登录";
    self.titleMainLb.textColor = RGBA(35, 24, 21, 1);
    self.titleMainLb.font = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:self.titleMainLb];
    [self.titleMainLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.top.equalTo(self.titleTotalLb.mas_bottom).offset(28);
    }];
    
    self.titleDescLb = [[UILabel alloc]init];
    self.titleDescLb.text = @"一旦注销，您将永久无法使用本帐号登录DDup相关服务平台，且会解除第三方帐号的绑定关系。";
    self.titleDescLb.textColor = C102;
    self.titleDescLb.font = font(14);
    self.titleDescLb.numberOfLines = 0;
    [self.view addSubview:self.titleDescLb];
    [self.titleDescLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.top.equalTo(self.titleMainLb.mas_bottom).offset(16);
    }];
    
    self.titleSubTitleLb = [[UILabel alloc]init];
    self.titleSubTitleLb.text = @"清空数据，无法恢复";
    self.titleSubTitleLb.textColor = RGBA(35, 24, 21, 1);
    self.titleSubTitleLb.font = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:self.titleSubTitleLb];
    [self.titleSubTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.top.equalTo(self.titleDescLb.mas_bottom).offset(28);
    }];
    
    self.titleSubDescLb = [[UILabel alloc]init];
    self.titleSubDescLb.text = @"注销后将永远清空DDup等相关平台的所有数据，包括个人资料等。";
    self.titleSubDescLb.textColor = C102;
    self.titleSubDescLb.font = font(14);
    self.titleSubDescLb.numberOfLines = 0;
    [self.view addSubview:self.titleSubDescLb];
    [self.titleSubDescLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(16);
        make.right.equalTo(self.view).offset(-16);
        make.top.equalTo(self.titleSubTitleLb.mas_bottom).offset(16);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    self.confirmBtn.layer.cornerRadius = 22;
    self.confirmBtn.layer.masksToBounds = YES;
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
   
    [self.confirmBtn setBackgroundImage:[UIImage imageNamed:@"login_confirm_normal_tag"] forState:UIControlStateNormal];
    [self.confirmBtn setBackgroundImage:[UIImage imageNamed:@"login_confirm_selected_tag"] forState:UIControlStateSelected];
    
    [self.confirmBtn setTitle:@"申请注销帐号" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:RGBA(255, 255, 255, 1) forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:RGBA(255, 255, 255, 1) forState:UIControlStateSelected];
    self.confirmBtn.titleLabel.font = font(18);
    self.confirmBtn.userInteractionEnabled = NO;
    [self.view addSubview:self.confirmBtn];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-(30 + GK_SAVEAREA_BTM));
        make.height.mas_equalTo(44);
        make.left.equalTo(self.view).offset(24);
        make.right.equalTo(self.view).offset(-24);
    }];
    self.checkImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_select_tag"]];
    self.checkImgView.highlightedImage = [UIImage imageNamed:@"common_selected_tag"];
    [self.view addSubview:self.checkImgView];
    [self.checkImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.confirmBtn.mas_top).offset(-28);
        make.left.equalTo(self.view).offset(24);
        make.size.mas_equalTo(CGSizeMake(14, 14));
    }];
    self.remindTitleLb = [[UILabel alloc]init];
    self.remindTitleLb.text = @"我已清楚风险，确定继续申请注销帐号";
    self.remindTitleLb.textColor = C153;
    self.remindTitleLb.font = font(12);
    [self.view addSubview:self.remindTitleLb];
    [self.remindTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkImgView.mas_right).offset(8);
        make.centerY.equalTo(self.checkImgView);
    }];
    self.checkBtn = [[UIButton alloc]init];
    [self.checkBtn addTarget:self action:@selector(checkMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.checkBtn];
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.checkImgView);
        make.right.equalTo(self.remindTitleLb.mas_right);
    }];
    
}

- (void)confirmMethod{
    [SVProgressHUD show];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = [RPAppManager getCustomerId];
    param[@"md5"] = [RPAppManager md5StrWithApi:DeleteUser];
    [[RPNetworkExtensionTool sharedTool] requestPOST:kGET_REQUEST_URL(DeleteUser) parames:param success:^(id responseObj) {
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"CLDeviceToken"] length] != 0) {
        
            // 查询用户对象
            UserInfoModel *userInfo = [UserInfoModel userInfo];
            // 删除手机相关信息
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:userInfo.phone];
            // 同步
            [[NSUserDefaults standardUserDefaults]synchronize];
            // 删除用户信息
            [UserInfoModel deleteUserInfo];
            
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            param[@"md5"] = [RPAppManager md5StrWithApi:UpdateDeviceToken];
            param[@"userId"] = @0;
            param[@"deviceType"] = @2;
            param[@"deviceToken"] = [[NSUserDefaults standardUserDefaults]objectForKey:@"CLDeviceToken"];
            [[RPNetworkExtensionTool sharedTool]requestPOST:kGET_REQUEST_URL(UpdateDeviceToken) parames:param success:^(id responseObj) {
                // 删除退出用户
                [SVProgressHUD dismiss];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"removeTimer" object:nil];
                
                [WHToast showImage:[UIImage imageNamed:@"toast_tag_success"] message:@"注销成功" duration:1 finishHandler:^{
                    DiscoverListViewController *listVC = [[DiscoverListViewController alloc]init];
                    NavigationViewController *nav = [[NavigationViewController alloc]initWithRootViewController:listVC];
                    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
                }];

               
            } failure:^(id error) {
                            
            }];
        }else{
            [SVProgressHUD dismiss];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"removeTimer" object:nil];
            
            [WHToast showImage:[UIImage imageNamed:@"toast_tag_success"] message:@"注销成功" duration:1 finishHandler:^{
                DiscoverListViewController *listVC = [[DiscoverListViewController alloc]init];
                NavigationViewController *nav = [[NavigationViewController alloc]initWithRootViewController:listVC];
                [UIApplication sharedApplication].keyWindow.rootViewController = nav;
            }];
            UserInfoModel *userInfo = [UserInfoModel userInfo];
            
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:userInfo.phone];
            
            [[NSUserDefaults standardUserDefaults]synchronize];
            // 删除用户信息
            [UserInfoModel deleteUserInfo];
        }
    } failure:^(NSError *error) {
        [WHToast showImage:[UIImage imageNamed:@"toast_tag_error"] message:error.userInfo[@"info"] duration:2 finishHandler:nil];
    }];
}
- (void)checkMethod{
    self.checkImgView.highlighted = !self.checkImgView.highlighted;
    self.confirmBtn.userInteractionEnabled = self.checkImgView.highlighted;
    
    self.confirmBtn.selected = self.checkImgView.highlighted;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
